home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Snippets / EMBL Search / Sources / browse.c < prev    next >
Encoding:
Text File  |  1992-05-04  |  23.9 KB  |  270 lines  |  [TEXT/KAHL]

  1.         *itemHit = OK;
  2.                     }
  3.                     else {
  4.                         /* if click is not on the same line as currently selected */
  5.                         if(browsePtr->screentop+line != browsePtr->selected) {
  6.                             /* unhilite old selection if visible */
  7.                             if(browsePtr->selected >= browsePtr->screentop &&
  8.                                 browsePtr->selected < browsePtr->screentop+BROWSERLINES) {
  9.                                 box = textR;
  10.                                 box.top += (browsePtr->selected-browsePtr->screentop)*fMonaco.height;
  11.                                 box.bottom = box.top + fMonaco.height;
  12.                                 DoInvertRect(&box);
  13.                             }
  14.                             
  15.                             /* hilite new selection */
  16.                             browsePtr->selected = browsePtr->screentop+line;
  17.                             box = textR;
  18.                             box.top += (browsePtr->selected-browsePtr->screentop)*fMonaco.height;
  19.                             box.bottom = box.top + fMonaco.height;
  20.                             DoInvertRect(&box);
  21.         
  22.                             horiz = textR.left + BROWSE_MARGIN +
  23.                                     (BROWSERTEXTLEN + 1)*fMonaco.finfo.widMax;
  24.                             MoveTo(horiz-fMonaco.finfo.widMax/2,textR.top);
  25.                             LineTo(horiz-fMonaco.finfo.widMax/2,textR.bottom);
  26.                         }
  27.                     }
  28.                     ret = TRUE;
  29.                 }        
  30.             }
  31.             else {        /* if user clicked into a control */
  32.                 /* click in scroll bar ? */
  33.                 GetDItem(myDialog,BROWSER_SCROLLBAR, &kind,(Handle *)&scrollH,
  34.                             &r);
  35.                 if( theControl == scrollH ) {
  36.                     oldScreentop = browsePtr->screentop;
  37.                     
  38.                     /* keep the old CtlValue for the following reason:
  39.                         If user moves the thumb we recover screentop by looking at CtlValue.
  40.                         Imagine user moved the thumb to the end of list. Then we set screentop
  41.                         to nrec-BROWSERLINES and set CtlValue to screentop/scale.
  42.                         Next time a user clicks on the thumb but doesn't move it, CtlValue
  43.                         is still the same but if we recalculated screentop = CtlValue * scale,
  44.                         we might get a different value for screentop due the the scaling
  45.                         effect (no floating point division is used for screentop/scale).
  46.                         Therefore, we only recalculate screentop if the user actually moved the
  47.                         thumb.
  48.                     */ 
  49.                     oldValue = GetCtlValue(scrollH);
  50.                     
  51.                     if(cntlCode == inThumb) {
  52.                         TrackControl(theControl, where, NULL);
  53.                         if(oldValue != GetCtlValue(scrollH)) {
  54.                             /* only if user moved thumb: */
  55.                             browsePtr->screentop = (u_long)GetCtlValue(scrollH)*browsePtr->scale;
  56.                             if( browsePtr->screentop < 0 )
  57.                                 browsePtr->screentop = 0;
  58.                             if( browsePtr->screentop > browsePtr->nrec-BROWSERLINES )
  59.                                 browsePtr->screentop = browsePtr->nrec-BROWSERLINES;
  60.                         
  61.                             if(oldScreentop != browsePtr->screentop) {
  62.                                  GetDItem(myDialog,BROWSER_USRITEM1,&kind,&h,&textR);
  63.                                 InvalRect(&textR);
  64.  
  65.                                 AdjustBrowserVScroll(myDialog);
  66.                             }
  67.                         }
  68.                     }
  69.                     else {    /* click in page up/down or arrow up/down control part */
  70.                         switch(cntlCode) {
  71.                             case inUpButton: gScrollAmt = -1;
  72.                                 break;
  73.                             case inDownButton: gScrollAmt = 1;
  74.                                 break;
  75.                             case inPageUp: gScrollAmt = -(BROWSERLINES-1);
  76.                                 break;
  77.                             case inPageDown: gScrollAmt = BROWSERLINES-1;
  78.                                 break;
  79.                         }
  80.                         gScrollCode=cntlCode;
  81.                         
  82.                         /* track user action and redraw if necessary */
  83.                         TrackControl(theControl, where, (ProcPtr)ScrollResProc);
  84.                     } /* end else (cntlCode == inThumb) */
  85.                                         
  86.                     *itemHit = 0; /* dummy */
  87.                     ret = TRUE;
  88.                 }
  89.             } /* end else FindControl */
  90.             
  91.             /* save relevant information for next click */
  92.             prevTime = theEvent->when;
  93.             prevLine = line;
  94.             SetPort(savePort);
  95.             return(ret);
  96.             break;
  97.   }
  98.    
  99.    return(FALSE);
  100. }
  101.  
  102. /**************************************
  103. *    Look through the index for a specified word.
  104. */
  105.  
  106. static void LocateWord(DialogPtr myDialog)
  107. {
  108.     short                kind;
  109.     Handle            h;
  110.     Rect                r;
  111.     short                size;
  112.     long                pos,count;
  113.     Ptr                recPtr;
  114.     short                (*compare)(void *,void *);
  115.     OSErr                err;
  116.     Boolean            bDone = FALSE;
  117.     Str255            findWord;
  118.     GrafPtr            savePort;
  119.     BrowseRecPtr    browsePtr;
  120.     
  121.     if( (browsePtr = (BrowseRecPtr)((WindowPeek)myDialog)->refCon) == NULL)
  122.         return;
  123.  
  124.     size = (long)browsePtr->recsize;    
  125.     if((recPtr = NewPtrClear((Size)size)) == NULL)
  126.         return;
  127.             
  128.     /* compare routine depends on database field to be searched */    
  129.     if(browsePtr->dbfield == ENAME_QRY)
  130.         compare = ename_compare;
  131.     else
  132.         compare = general_compare;
  133.  
  134.     /* get word to look for */
  135.     GetDlgText(myDialog,BROWSER_EDITFLD,findWord);
  136.     if(pstrlen(findWord) == 0)    return;
  137.     
  138.     PtoCstr(findWord);
  139.     str2upper((char *)findWord);
  140.     
  141.     /* do the binary search */
  142.     StartWaitCursor();
  143.     !CDIndex_BSearch(findWord,
  144.                 browsePtr->fName,
  145.                 browsePtr->fd,
  146.                 (u_long)browsePtr->recsize,
  147.                 (u_long)browsePtr->nrec,
  148.                 compare,
  149.                 recPtr, (u_long *)&pos);
  150.                 
  151.     /* now look backwards for first occurrence. Yes, this is inefficient, I know... */            
  152.     do {
  153.         --pos;
  154.         if( (err=SetFPos(browsePtr->fd,fsFromStart,
  155.                     pos*(long)browsePtr->recsize+sizeof(Header))) != noErr) {
  156.             sprintf(gError,LoadErrorStr(ERR_READFILE,FALSE),
  157.                     PtoCstr(browsePtr->fName),err );
  158.             CtoPstr((char *)browsePtr->fName);
  159.             ErrorMsg(0);
  160.             break;
  161.         }
  162.  
  163.         count = (long)browsePtr->recsize;
  164.         if( ReadMacFile(browsePtr->fd,&count,(void *)recPtr,browsePtr->fName,TRUE) ) {
  165.             break;
  166.         }
  167.             
  168.         if(compare(findWord,recPtr))
  169.             bDone = TRUE;
  170.     } while ( !bDone );
  171.     InitCursor();
  172.  
  173.     DisposPtr((Ptr)recPtr);
  174.     browsePtr->selected = browsePtr->screentop = pos + 1;
  175.     
  176.     /* adjust screen top */
  177.     if( browsePtr->screentop < 0 )
  178.         browsePtr->screentop = 0;
  179.     if( browsePtr->screentop > browsePtr->nrec-BROWSERLINES )
  180.         browsePtr->screentop = browsePtr->nrec-BROWSERLINES;
  181.         
  182.     savePort = ChangePort(myDialog);
  183.      GetDItem(myDialog,BROWSER_USRITEM1,&kind,&h,&r);
  184.     InvalRect(&r);
  185.  
  186.     AdjustBrowserVScroll(myDialog);
  187.  
  188.     SetPort(savePort);
  189. }
  190.  
  191. /**************************************
  192. *    This procedure continuously updates the window as long as user holds mouse down
  193. *    in scroll bar 
  194. */
  195.  
  196. static pascal void ScrollResProc(ControlHandle theControl, short theCode)
  197. {
  198.     WindowPtr        ctlWindow=(*theControl)->contrlOwner;
  199.     BrowseRecPtr    browsePtr;
  200.     long                oldScreentop;
  201.     
  202.     if(theCode == gScrollCode) {
  203.         browsePtr = (BrowseRecPtr)((WindowPeek)ctlWindow)->refCon;
  204.         oldScreentop = browsePtr->screentop;
  205.         
  206.         browsePtr->screentop += gScrollAmt;
  207.         /* limit check */
  208.         if( browsePtr->screentop < 0 )
  209.             browsePtr->screentop = 0;
  210.         if( browsePtr->screentop > browsePtr->nrec-BROWSERLINES )
  211.             browsePtr->screentop = browsePtr->nrec-BROWSERLINES;
  212.                 
  213.         /* only redraw if necessary. Avoids flickering at top and end of list */
  214.         if(oldScreentop != browsePtr->screentop) {
  215.             DrawBrowserAll(ctlWindow);
  216.             AdjustBrowserVScroll(ctlWindow);
  217.         }
  218.     }
  219. }
  220.  
  221.  
  222. /**************************************
  223. *    Adjust CtlValue according to current screentop value taking scaling into account
  224. */
  225.  
  226. static void AdjustBrowserVScroll(DialogPtr myDialog)
  227. {
  228.     short                kind;
  229.     Handle            h;
  230.     Rect                r;
  231.     BrowseRecPtr    browsePtr;
  232.  
  233.     if( (browsePtr = (BrowseRecPtr)((WindowPeek)myDialog)->refCon) == NULL)
  234.         return;
  235.         
  236.     GetDItem(myDialog,BROWSER_SCROLLBAR,&kind,&h,&r);    /* get handle to scroll bar */
  237.     SetCtlValue((ControlHandle)h,browsePtr->screentop/browsePtr->scale);
  238. }
  239.  
  240. /**************************************
  241. *    Show number of items in index
  242. */
  243.  
  244. static pascal void DrawNRec(WindowPtr myWindow,short itemNo)
  245. {
  246.     short                kind;
  247.     Handle            h;
  248.     Rect                r;
  249.     BrowseRecPtr    browsePtr;
  250.     Str255            text,str;
  251.     
  252.     if( (browsePtr = (BrowseRecPtr)((WindowPeek)myWindow)->refCon) == NULL)
  253.         return;
  254.         
  255.     GetDItem(myWindow,itemNo,&kind,&h,&r);    /* get drawing position */
  256.     
  257.     TextFont(fMonaco.num);
  258.     TextSize(9);
  259.     
  260.     NumToString(browsePtr->nrec,text);
  261.     GetIndString(str,OTHERS,ITEMSINLIST);
  262.     pstrcat(text,str);
  263.     
  264.     MoveTo(r.left,r.bottom);
  265.     DrawString(text);
  266.     
  267.     TextFont(fSystem.num);
  268.     TextSize(12);
  269. }
  270.